package com.xgr.wonderful.activity;


import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

import com.google.gson.Gson;
import com.markupartist.android.widget.ActionBar;
import com.markupartist.android.widget.ActionBar.Action;
import com.xgr.wonderful.R;
import com.xgr.wonderful.activity.base.BasePageActivity;
import com.xgr.wonderful.constant.Constant;
import com.xgr.wonderful.constant.HttpGetUrl;
import com.xgr.wonderful.entity.User;
import com.xgr.wonderful.utils.HttpProxy;
import com.xgr.wonderful.utils.LogUtils;
import com.xgr.wonderful.utils.SettingUtils;
import com.xgr.wonderful.utils.ToastFactory;
import com.xgr.wonderful.utils.HttpProxy.IHttpRequestListener;

/**
 * 可以得到用户的id/username。以重新对手机进行绑定
 * @author lsj
 *
 */
public class FindUserActivity  extends BasePageActivity{
	private ActionBar mActionBar ;
	private EditText mEditUserid ;
	private EditText mEditUsername ;
	
	private HttpProxy httpProxy ;
	
	private User mUser ;
	@Override
	protected void setLayoutView() {
		setContentView(R.layout.activity_finduser) ;
	}
	@Override
	protected void findViews() {
		mActionBar = (ActionBar)findViewById(R.id.actionbar_finduser) ;
		mEditUserid = (EditText)findViewById(R.id.finduser_id) ;
		mEditUsername = (EditText)findViewById(R.id.finduser_name) ;
	}

	@Override
	protected void setupViews(Bundle bundle) {
		mActionBar.setTitle("绑定新手机") ;
		mActionBar.setDisplayHomeAsUpEnabled(true) ;
		mActionBar.setHomeAction(new Action() {
			@Override
			public void performAction(View view) {
				finish() ;
			}
			
			@Override
			public int getDrawable() {
				return R.drawable.weix_logo_blank;
			}
		}) ;
		mActionBar.addAction(new Action() {	
			@Override
			public void performAction(View view) {
				//开始查找
				findUser();
			}		
			@Override
			public int getDrawable() {
				return R.drawable.ic_action_send_now;
			}
		}) ;
		
		httpProxy = new HttpProxy() ;
	}
	
	protected void findUser() {
		String userid = mEditUserid.getText().toString().trim() ;
		String username = mEditUsername.getText().toString().trim() ;
		if (userid==null ||username== null|| userid.length()==0|| username.length()==0){
			ToastFactory.getToast(mContext, "请同时输入id和用户名").show() ;
			return ;
		}
		//下面要进行用户名与 userid
		checkUserExist(userid, username) ;
		
	}
	/**
	 * 通过userid得到username ，再对比输入的 username 
	 * @param userid
	 * @param username
	 * @return
	 */
	private void checkUserExist(final String userid, final String username){
		String url = HttpGetUrl.GetUser() + "?id="+ userid.toString();
		httpProxy.queryUserByUserID(url, new IHttpRequestListener() {
			
			@Override
			public void OnHttpRequestSuccessful(String result, int errCode) {
				if (errCode == Constant.ERR_HTTP_REQUEST_SUCCESS){
					User oldUser = null ;
					try {
						oldUser = new Gson().fromJson(result, User.class) ;
					} catch (Exception e) {}
					if (oldUser != null && oldUser.getUsername()!= null){
						//验证用户名是否正确 
						if (oldUser.getUsername().equals(username)) {	
							showProgressDialog("正在修改用户信息...", true) ;
							String deviceid = SettingUtils.getDeviceId(mContext) ;
							updateUser(userid, "111111") ;
						}else {
							cancelProgressDialog() ;
							ToastFactory.getToast(mContext, "输入有误").show() ;
						}
					}
				}else if (errCode == Constant.ERR_HTTP_REQUEST_FAILURE) {
					cancelProgressDialog() ;
					ToastFactory.getToast(mContext, result).show() ;
				}
			}
			
			@Override
			public void OnHttpRequestStart() {
				showProgressDialog("正在验证用户信息...", true) ;
			}
			
			@Override
			public void OnHttpRequestFailure(String info) {
				LogUtils.i(TAG, info) ;
				cancelProgressDialog() ;
				ToastFactory.getToast(mContext, "请查看您的网络").show() ;
			}
		}) ;
	}
	/**
	 * 主要 是更新用户的deviceid,绑定成功后，
	 */
	private void updateUser(String userid, String deviceid ){
		User newUser = new User(Integer.valueOf(userid),
				deviceid, null, null, null); 
		String userJson = new Gson().toJson(newUser) ;
		
		httpProxy.updateUser(userJson, new IHttpRequestListener() {
			
			@Override
			public void OnHttpRequestSuccessful(String result, int errCode) {
				if (errCode == Constant.ERR_HTTP_REQUEST_SUCCESS){
					ToastFactory.getToast(mContext, "重新绑定成功").show() ;
					cancelProgressDialog() ;
					finish() ;//返回以直接登录
				}else if (errCode == Constant.ERR_HTTP_REQUEST_FAILURE) {
					cancelProgressDialog() ;
					ToastFactory.getToast(mContext, "重新绑定失败"+result).show() ;
				}
			}
			
			@Override
			public void OnHttpRequestStart() {
				showProgressDialog("正在重新绑定手机", false) ;
			}
			
			@Override
			public void OnHttpRequestFailure(String info) {
				cancelProgressDialog() ;
				LogUtils.i(TAG, "fail info:"+ info);
				ToastFactory.getToast(mContext, "重新绑定失败").show() ;
			}
		}) ;
	}

	@Override
	protected void setListener() {
		// TODO Auto-generated method stub
		
	}

}
